package com.iot08.api.service;

import com.aliyun.tea.utils.StringUtils;
import com.iot08.api.response.VipPrivilegeResp;
import com.iot08.api.vo.request.AddVipPrililegeReqVo;
import com.iot08.api.vo.request.EditVipPrililegeReqVo;
import com.iot08.common.entity.app.VipPrivilege;
import com.iot08.common.service.base.BaseAdminService;
import com.iot08.common.utils.StringUtil;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author cwj
 * @description
 * @date 2024/12/29 9:44
 **/
@Slf4j
@Service
public class VipPrivilegeService extends BaseAdminService<VipPrivilege> {
    
    /**
     * @description 查询会员特权信息及会员提示
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2024/12/29 15:24
     **/
    public ResponseMap queryVipPrivilege(){
        ResponseMap res = ResponseMap.success();
        List<VipPrivilegeResp> vipPrivilegeResps = mongoTemplate.findAll(VipPrivilegeResp.class,"vip_privilege");
        res.put("vipPrivileges",vipPrivilegeResps);
        return res;
    }

    /**
     * @description 查询所有特权信息
     * @author cwj
     * @param []
     * @return java.util.List<com.iot08.common.entity.app.VipPrivilege>
     * @date @time 2024/12/31 10:29
     **/
    public List<VipPrivilege> queryAllVipPrivilege(){
       Query query = new Query().with(Sort.by(Sort.Order.asc("createTime")));
       //return mongoTemplate.findAll(getTClass());
        return this.find(query);
    }

    /**
     * @description 查询所有特权信息
     * @author cwj
     * @param []
     * @return java.util.List<com.iot08.api.response.VipPrivilegeResp>
     * @date @time 2025/4/23 15:10
     **/
    public List<VipPrivilegeResp> queryAllVipPrivilegeResp(){
        Query query = new Query().with(Sort.by(Sort.Order.asc("createTime")));
        return mongoTemplate.find(query,VipPrivilegeResp.class,"vip_privilege");
    }

    /**
     * @description 添加会员特权说明
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2024/12/29 15:24
     **/
    public ResponseMap addVipPrililegeInfo(AddVipPrililegeReqVo vo){
        VipPrivilege vipPrivilege = new VipPrivilege();
        vipPrivilege.setName(vo.getName());
        vipPrivilege.setDesc(vo.getDesc());
        vipPrivilege.setIcon(vo.getIcon());
        vipPrivilege.setIconUrl(vo.getIconUrl());
        vipPrivilege.setCreateTime(new Date());
        vipPrivilege.setUpdateTime(new Date());
        this.save(vipPrivilege);
        return ResponseMap.success();
    }


    /***
     * @description 编辑会员信息
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/2/15 11:39
     **/
    public ResponseMap editVipVipPrililege(EditVipPrililegeReqVo vo){
        Update update = new Update();
        if(!StringUtils.isEmpty(vo.getName())) {
            update.set("name",vo.getName());
        }
        if(!StringUtils.isEmpty(vo.getIcon())) {
            update.set("icon",vo.getIcon());
        }
        if(!StringUtils.isEmpty(vo.getIconUrl())) {
            update.set("iconUrl",vo.getIconUrl());
        }
        if(!StringUtils.isEmpty(vo.getDesc())) {
            update.set("desc",vo.getDesc());
        }
        this.updateById(new ObjectId(vo.getId()),update);
        return ResponseMap.success();
    }
}
